﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using SharePointSearchBench.Library;
using System.Collections.Specialized;
using System.Collections.ObjectModel;

namespace SharePointSearchBench.Tests
{
    [TestClass()]
    public class FullTextBuilderTest
    {
        [TestMethod()]
        public void TestSelect()
        {
            string field1, field2, keywords, order;
            field1 = "blah";
            field2 = "asdf";
            keywords = "llama dog cat";
            order = "rank";
            string input = "{0}, {1}";
            input = string.Format(input, field1, field2);
            FullTextBuilder output = FullTextBuilder.Select(input).Where(Condition.FreeText(keywords));

            string expected = "SELECT {0} FROM Scope() WHERE (FREETEXT(DEFAULTPROPERTIES, '{1}')) ORDER BY RANK DESC";
            expected = string.Format(expected, input, keywords, order);
            Assert.AreEqual(expected, output.ToString());
        }

        [TestMethod()]
        public void TestBasicWhereClause()
        {
            string field1, field2, where, order;
            field1 = "blah";
            field2 = "asdf";
            where = "FREETEXT(DEFAULTPROPERTIES, 'llama cat dog')";
            order = "rank";
            string input = "{0}, {1}";
            input = string.Format(input, field1, field2);
            FullTextBuilder output = FullTextBuilder.Select(input).Where(where);

            string expected = "SELECT {0} FROM Scope() WHERE {1} ORDER BY RANK DESC";
            expected = string.Format(expected, input, where, order);
            Assert.AreEqual(expected, output.ToString());
        }

        [TestMethod()]
        public void TestSelectStringCollection()
        {
            string field1, field2, keywords, order;
            field1 = "blah";
            field2 = "asdf";
            StringCollection fields = new StringCollection();
            fields.Add(field1);
            fields.Add(field2);

            keywords = "llama dog cat";
            order = "rank";
            FullTextBuilder output = FullTextBuilder.Select(fields).Where(Condition.FreeText(keywords));

            string selectInput = string.Empty;
            foreach(string field in fields)
            {
                selectInput = selectInput + field + ", ";
            }

            selectInput = selectInput.Substring(0, selectInput.Length - 2);

            string expected = "SELECT {0} FROM Scope() WHERE (FREETEXT(DEFAULTPROPERTIES, '{1}')) ORDER BY RANK DESC";
            expected = string.Format(expected, selectInput, keywords, order);
            Assert.AreEqual(expected, output.ToString());
        }

        [TestMethod()]
        public void TestSelectPropertyCollection()
        {
            Search search = SearchTest.GetTestSearchObject();
            TestSearchBenchContext context = new TestSearchBenchContext(search);
            Collection<SearchProperty> properties = context.GetSearchProperties();

            string keywords = "llama dog cat";
            string order = "rank";
            FullTextBuilder output = FullTextBuilder.Select(properties).Where(Condition.FreeText(keywords));

            string selectInput = string.Empty;
            foreach(SearchProperty field in properties)
            {
                selectInput = selectInput + field.Name + ", ";
            }

            selectInput = selectInput.Substring(0, selectInput.Length - 2);

            string expected = "SELECT {0} FROM Scope() WHERE (FREETEXT(DEFAULTPROPERTIES, '{1}')) ORDER BY RANK DESC";
            expected = string.Format(expected, selectInput, keywords, order);
            Assert.AreEqual(expected, output.ToString());
        }


    }
}
